package com.mentalhealthplatform.studentV3.controller;

import com.mentalhealthplatform.appModels.BookingLecture;
import com.mentalhealthplatform.appModels.Lecture;
import com.mentalhealthplatform.studentV3.service.BookingLectureServiceV3;
import com.mentalhealthplatform.studentV3.service.LectureServiceV3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v3/lectures")
public class LectureControllerV3 {

    @Autowired
    private LectureServiceV3 lectureServiceV3;

    @Autowired
    private BookingLectureServiceV3 bookingLectureServiceV3;

    // 获取所有讲座
    @GetMapping
    public ResponseEntity<List<Lecture>> getAllLectures() {
        List<Lecture> lectures = lectureServiceV3.findAll();
        return ResponseEntity.ok(lectures);
    }

    // 根据讲座 ID 获取单个讲座
    @GetMapping("/{id}")
    public ResponseEntity<Lecture> getLectureById(@PathVariable("id") Long id) {
        return lectureServiceV3.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // 创建讲座
    @PostMapping
    public ResponseEntity<Lecture> createLecture(@RequestBody Lecture lecture) {
        Lecture savedLecture = lectureServiceV3.save(lecture);
        return ResponseEntity.status(201).body(savedLecture); // 返回 201 Created
    }

    // 删除讲座（可选，如果需要可以取消注释）
    // @DeleteMapping("/{id}")
    // public ResponseEntity<Void> deleteLecture(@PathVariable("id") Long id) {
    //     if (lectureService.existsById(id)) {
    //         lectureService.deleteById(id);
    //         return ResponseEntity.noContent().build(); // 返回 204 No Content
    //     } else {
    //         return ResponseEntity.notFound().build(); // 返回 404 Not Found
    //     }
    // }

    // 创建预约
    @PostMapping("/bookings")
    public ResponseEntity<BookingLecture> createBooking(@RequestBody BookingLecture bookingLecture) {
        BookingLecture savedBooking = bookingLectureServiceV3.createBookingLecture(bookingLecture);
        return ResponseEntity.status(201).body(savedBooking); // 返回 201 Created
    }

    // 根据电话查询预约信息
    @GetMapping("/bookings")
    public ResponseEntity<List<BookingLecture>> getBookiyPhone(@RequestParam String phone) {
        List<BookingLecture> bookings = bookingLectureServiceV3.findByPhone(phone);
        if (bookings.isEmpty()) {
            return ResponseEntity.notFound().build(); // 如果没有找到预约记录，返回 404 状态
        }
        return ResponseEntity.ok(bookings); // 返回找到的预约记录
    }


    // 根据讲座 ID 查询预约信息
    @GetMapping("/{id}/bookings")
    public ResponseEntity<List<BookingLecture>> getBookingsByLectureId(@PathVariable("id") Long lectureId) {
        List<BookingLecture> bookings = bookingLectureServiceV3.findByLectureId(lectureId);
        if (bookings.isEmpty()) {
            return ResponseEntity.notFound().build(); // 如果没有找到预约记录，返回 404 状态
        }
        return ResponseEntity.ok(bookings); // 返回找到的预约记录
    }

    // 根据预约 ID 获取预约信息

    @GetMapping("/bookings/{id}")
    public ResponseEntity<BookingLecture> getBookingById(@PathVariable Long id) {
        try {
            // 从服务层获取预约信息
            BookingLecture bookingLecture = bookingLectureServiceV3.findById(id);

            if (bookingLecture != null) { // 检查预约记录是否存在
                return ResponseEntity.ok(bookingLecture); // 返回 200 OK 和预约信息
            } else {
                return ResponseEntity.notFound().build(); // 返回 404 Not Found
            }
        } catch (IllegalArgumentException e) {
            // 如果找不到预约记录，返回 404 Not Found
            return ResponseEntity.notFound().build();
        } catch (Exception e) {
            // 处理其他异常，返回 500 Internal Server Error
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }


    // 删除预约记录
    @DeleteMapping("/bookings/{id}")
    public ResponseEntity<Void> deleteBooking(@PathVariable Long id) {
        if (bookingLectureServiceV3.existsById(id)) { // 检查预约记录是否存在
            bookingLectureServiceV3.deleteById(id); // 删除预约记录
            return ResponseEntity.noContent().build(); // 返回 204 No Content
        } else {
            return ResponseEntity.notFound().build(); // 返回 404 Not Found
        }
    }

    @GetMapping("/search")
    public ResponseEntity<List<Lecture>> searchLectures(@RequestParam String text) {
        List<Lecture> lectures = lectureServiceV3.searchByText(text);
        return ResponseEntity.ok(lectures);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteLecture(@PathVariable("id") Long id) {
        if (lectureServiceV3.findById(id).isPresent()) {
            lectureServiceV3.deleteById(id);
            return ResponseEntity.noContent().build(); // 返回 204 No Content
        } else {
            return ResponseEntity.notFound().build(); // 返回 404 Not Found
        }
    }

}
